<script setup>
//质检员管理
import { ref, watch, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { getInspectorPage } from '@/api/inspector'

onMounted(() => {
  search()
})
const search = async () => {
  try {
    const result = await getInspectorPage(pageNo.value, pageSize.value)
    
    if (result.data.code == 200) {
          console.log('获取用户信息成功')
          console.log(result.data)
          inspectorList.value = result.data.data.records
          total.value = result.data.data.total
    } 
    else {
      ElMessage.error('数据加载失败')
    }

  } catch (error) {
    ElMessage.error('网络请求失败')
  }
}
const inspectorList = ref([])
const pageNo = ref(1);
const pageSize = ref(10);
const background = ref(true)
const total = ref(0)
const handleSizeChange = (val) => {
  // 添加边界校验
  if (val < 1) val = 1;
  pageSize.value = val;
  pageNo.value = 1; // 重置到第一页
  search();
};

const handleCurrentChange = (val) => {
  // 添加边界校验
  if (val < 1) val = 1;
  if (val > Math.ceil(total.value / pageSize.value)) {
    val = Math.ceil(total.value / pageSize.value);
  }
  pageNo.value = val;
  search();
};
</script>

<template>

    <div>
      <div id="title">质检员管理</div><br>
    </div>
  <el-table :data="inspectorList" border style="width: 100%" >
    <el-table-column prop="username" label="用户名" width="120" align="center"></el-table-column>
    <el-table-column prop="job" label="职位" width="120" align="center"></el-table-column>
    <el-table-column prop="telephone" label="电话" width="200" align="center"></el-table-column>
    <el-table-column prop="email" label="邮箱" width="250" align="center"></el-table-column>
    <el-table-column prop="updatedAt" label="修改时间" width="250" align="center"></el-table-column>
    <el-table-column label="状态" width="120" align="center">
  <template #default="scope">
    <el-button v-if="scope.row.enabled" type="success" plain>启用中</el-button>
    <el-button v-else type="danger" plain>禁用中</el-button>
  </template>
    </el-table-column>
    <el-table-column label="操作" fixed="right" align="center">
      <template #default="scope">
        <el-button size="small" type="primary" @click="">任用</el-button>
        <el-button size="small" type="danger" @click="">移除</el-button>
      </template>
    </el-table-column>
  </el-table>
  <br>
    <br>
    <div class="container">
      <el-pagination
      v-model:current-page="pageNo"
      v-model:page-size="pageSize"
      :page-sizes="[5, 10, 20, 50]"
      :background="background"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    </div>
 
</template>

<style scoped>
#title {
  font-size: 20px;
  font-weight: 600;
}
</style>